﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using XYTXL.Data;
using XYTXL.API;

namespace XYTXL.Web.View
{
    public partial class CWPay : System.Web.UI.Page
    {




        public string sign;
        public string sysid;
        public string data;
        public string subsysid;
        string cert = "601CCD54D7EEF7A3E050230A0164565F601CCD54D7EFF7A3E050230A0164565F601CCD54D7F0F7A3E050230A0164565F601CCD54D7F1F7A3E050230A0164565F601CCD54D7F2F7A3E050230A0164565F601CCD54D7F3F7A3E050230A0164565F601CCD54D7F4F7A3E050230A0164565F601CCD54D7F5F7A3E050230A0164565F601CCD54D7F6F7A3E050230A0164565F601CCD54D7F7F7A3E050230A0164565F601CCD54D7F8F7A3E050230A0164565F601CCD54D7F9F7A3E050230A0164565F601CCD54D7FAF7A3E050230A0164565F601CCD54D7FBF7A3E050230A0164565F601CCD54D7FCF7A3E050230A0164565F601CCD54D7FDF7A3E050230A0164565F601CCD54D7FEF7A3E050230A0164565F601CCD54D7FFF7A3E050230A0164565F601CCD54D800F7A3E050230A0164565F601CCD54D801F7A3E050230A0164565F601CCD54D802F7A3E050230A0164565F601CCD54D803F7A3E050230A0164565F601CCD54D804F7A3E050230A0164565F601CCD54D805F7A3E050230A0164565F601CCD54D806F7A3E050230A0164565F601CCD54D807F7A3E050230A0164565F601CCD54D808F7A3E050230A0164565F601CCD54D809F7A3E050230A0164565F601CCD54D80AF7A3E050230A0164565F601CCD54D80BF7A3E050230A0164565F601CCD54D80CF7A3E050230A0164565F601CCD54D80DF7A3E050230A0164565F";

        protected void Page_Load(object sender, EventArgs e)
        {



            int strDDH =int.Parse( Request["ddh"].ToString());
            xy_jzmx mode = new xy_jzmxB().GetEntity(d => d.ddh == strDDH);
            string ddh = mode.ddh.ToString();
            string name = mode.name.ToString();
            double je =double.Parse( mode.je.ToString ());
            string id = mode.ID.ToString();
            string xml = "<?xml version=\'1.0\' encoding=\'GBK\'?><billinfo><version>1.0.0.2</version><billno>" + ddh + "</billno><orderinfono>" + ddh + "</orderinfono><orderinfoname>" + name + "</orderinfoname><returnURL>http://xyxt.wxit.edu.cn/View/CWReturn.aspx</returnURL><billdtl><feeitemid>201703-01</feeitemid><feeord >" + id + "</feeord><amt>" + je + "</amt></billdtl></billinfo>";


            data = LanChange(xml);
            XYTXL.API.CommonHelp.WriteLOG("华丽的分割线————分割线");
            XYTXL.API.CommonHelp.WriteLOG("1-开始序列化xml数据data：" + xml);
            sysid = "08";
            subsysid = "201703";
            XYTXL.API.CommonHelp.WriteLOG("2-sysid：" + sysid);
            XYTXL.API.CommonHelp.WriteLOG("2-subsysid：" + subsysid);
            string st = sysid + subsysid + cert + xml;
            XYTXL.API.CommonHelp.WriteLOG("3-生成校验数据：" + st);

            sign = GetMd5Hash(LanChange(st));

            // string a = GetMd5Hash("a");
            // string abc = GetMd5Hash("abc");
            // string az = GetMd5Hash("abcdefghijklmnopqrstuvwxyz");
            // string mg = GetMd5Hash("message digest");
            //// string d1 = GetMd5Hash(d);

            XYTXL.API.CommonHelp.WriteLOG("4-MD5生成签名sign-GetMd5Hash（data）：" + data);
            XYTXL.API.CommonHelp.WriteLOG("4-MD5生成签名sign-GetMd5Hash（sign）：" + sign);

        }
        public static string Getmd5(string str)
        {
            //创建MD5对像
            MD5 md5 = MD5.Create();
            //将字符串转换成数组
            byte[] ba = Encoding.UTF8.GetBytes(str);
            //将数组加密 成  加密数组
            byte[] md55 = md5.ComputeHash(ba);
            //将加密数组编译成字符串
            // return Encoding.Default.GetString(md55);
            //
            string STR = "";
            //便利数组中元素转化成字符并拼接
            for (int I = 0; I < md55.Length; I++)
            {
                //X 表是10进制,X2表示16进制

                STR += md55[I].ToString("x2");

            }
            return STR;
        }

        public static string GetMd5Hash(string input)
        {
            //MD5 md5Hash = new MD5();
            MD5 md5Hash = MD5.Create();
            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }

        public string GetMD51(string temp)
        {
            MD5 md5 = System.Security.Cryptography.MD5.Create();
            byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(temp);
            byte[] hash = md5.ComputeHash(inputBytes);

            // step 2, convert byte array to hex string
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("x2"));
            }
            return sb.ToString();
        }

        string LanChange(string str)
        {
            System.Text.Encoding utf8, gbk;
            utf8 = System.Text.Encoding.GetEncoding("UTF-8");
            gbk = System.Text.Encoding.GetEncoding("GBK");
            byte[] gb;
            gb = gbk.GetBytes(str);
            gb = System.Text.Encoding.Convert(gbk, utf8, gb);

            return utf8.GetString(gb);
        }
    }
}